<?php
require_once (dirname(__FILE__) . '/functions.php');

$file_type = $_POST['type'];
// For PHP files:
$file_name = $_POST['filename'];

if ($file_type == 'css') {
  $file_content = tf_print_css_code(0);
  $file_extension = 'css';
  $content_type = 'text/css';
} else if ($file_type == 'js') {
  $file_content = tf_print_js();
  $file_extension = 'js';
  $content_type = 'application/javascript';
} else if ($file_type == 'settings') {
  $file_content = serialize($thfr_css);
  $file_extension = 'txt';
  $content_type = 'text/plain';
} else if ($file_type == 'php') {
  if ($file_name == 'functions') {
    $file_content = tf_create_functions_file();
  } elseif ($file_name == 'header') {
    $file_content = tf_create_header_file();
  } elseif ($file_name == 'footer') {
    $file_content = tf_create_footer_file();
  } else {
    $file_content = tf_create_php_file($file_name);
  }
  $file_extension = 'php';
  $content_type = 'text/plain';
} else if ($file_type == 'zip') {
  $file_content = tf_create_php_file($file_name);
  $file_extension = 'php';
  $content_type = 'text/plain';
}

if ($file_type == 'php') {
  $filename = $file_name . '.php';
} elseif ($file_type == 'css') {
  $filename = 'style.css';
} elseif ($file_type == 'js') {
  $filename = 'javascript.js';
} else {
  if ($_POST['in_output']) {
    $filename = 'thfr-' . $file_type . '-' . str_replace('.', '', $_SERVER['SERVER_NAME']) . '-' . date('y') . '-' . date('m') . '-' . date('d') . '.' . $file_extension;
  }
  file_put_contents(dirname(__FILE__) . "/ready_themes/" . $thfr_css['themename'] . '.' . $file_extension, $file_content);
}
if ($_POST['in_output']) {
  // output the header
  header('Content-Description: File Transfer');
  header("Content-Disposition: attachment; filename=\"$filename\"");
  header('Content-Type: ' . $content_type . '; charset=utf-8');

  // output the file
  echo $file_content;
  die();
}
header('Location: ' . THFRURL);
?>